package com.taichuan.selfcheck.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.taichuan.selfcheck.eventbus.UsbConnectStateEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2021/6/30
 */
public class UsbConnectReceiver extends BroadcastReceiver {
    private static final String TAG = UsbConnectReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null) {
            if (action.equals("android.hardware.usb.action.USB_STATE")) {            //USB连接状态 广播
                //获取携带参数
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    try {
                        boolean isConnected = bundle.getBoolean("connected");
                        boolean isHostConnected = bundle.getBoolean("host_connected");
                        boolean isAdb = bundle.getBoolean("adb");
                        boolean configured = bundle.getBoolean("configured");
                        Log.e(TAG, "onReceive: 获取到USB连接状态广播，当前USB是否连接： " + isConnected + " | 是否以主机模式进行连接： " + isHostConnected
                                + " | 是否连接了Adb模式： " + isAdb + " | 是否有配置信息： " + configured);

                        //发送给外部组件
                        EventBus.getDefault().post(new UsbConnectStateEvent(isConnected,isHostConnected,isAdb,configured));
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e(TAG, "获取到USB连接状态广播中的携带参数异常： " + e.getMessage());
                    }
                }
            }
        }

    }
}
